package com.wing.web.db.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.wing.web.db.config.util.DataSourceHolder;
import com.wing.web.db.config.util.DataSourceKey;
import com.wing.web.db.config.util.DynamicDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author 作者 owen E-mail: 624191343@qq.com
 * @version 创建时间：2017年04月23日 下午20:01:06 类说明
 * central-platform
 * 在设置了spring.datasource.enable.dynamic 等于true是开启多数据源
 */
@Configuration
@ConditionalOnProperty(name = {"spring.datasource.dynamic.enable"}, matchIfMissing = false, havingValue = "true")
public class DataSourceConfig {

//	# Druid 数据源 1 配置，继承spring.datasource.druid.* 配置，相同则覆盖
//	...
//	spring.datasource.druid.one.max-active=10
//	spring.datasource.druid.one.max-wait=10000
//	...
//
//	# Druid 数据源 2 配置，继承spring.datasource.druid.* 配置，相同则覆盖
//	...
//	spring.datasource.druid.two.max-active=20
//	spring.datasource.druid.two.max-wait=20000
//	...
//	强烈注意：Spring Boot 2.X 版本不再支持配置继承，多数据源的话每个数据源的所有配置都需要单独配置，否则配置不会生效

//	创建数据源
//	所有引入db-core的模块都需要一个核心库，可以是user-center，也可以是oauth-center,file-center ,sms-center
	@Bean
//	@ConfigurationProperties("spring.datasource.druid.core")
	@ConfigurationProperties("spring.datasource.druid")
	public DataSource dataSourceCore(){
	    return DruidDataSourceBuilder.create().build();
	}

    /*@Bean
    @ConfigurationProperties("spring.datasource.druid.e0001")
    public DataSource dataSourceE0001(){
        return DruidDataSourceBuilder.create().build();
    }*/


    @Bean // 只需要纳入动态数据源到spring容器
    @Primary
    public DataSource dataSource() {
        DynamicDataSource dataSource = new DynamicDataSource();
        DataSource coreDataSource = this.dataSourceCore();
//        dataSource.addDataSource(DataSourceKey.core.name(), coreDataSource);
//        dataSource.setDefaultTargetDataSource(coreDataSource);
        DataSourceHolder.setDataSourceKey(DataSourceKey.core.name());
        return dataSource;
    }

    public static void main(String[] args){
        System.out.println(DataSourceKey.core.name());
    }


//    @Bean(name = "sqlSessionFactory")
//    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource)
//            throws Exception {
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//        bean.setDataSource(dataSource);
//
//        //加载mybatis配置文件
//        bean.setConfigLocation(new ClassPathResource("mybatis.cfg.xml"));
//
//        // 添加XML目录
//        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        try {
//            bean.setMapperLocations(resolver.getResources("classpath*:com/open/**/dao/*.xml"));
//            return bean.getObject();
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException(e);
//        }
//    }

//	@Bean
//	public MybatisSqlSessionFactoryBean sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource)
//			throws Exception {
//		MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
//		bean.setDataSource(dataSource);
//		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/open/**/dao/*.xml"));
//		bean.setTypeAliasesPackage("com.otc.*.dao");
//		MybatisConfiguration configuration = new MybatisConfiguration();
//		configuration.setMapUnderscoreToCamelCase(true);
//		configuration.setUseGeneratedKeys(true);
//		bean.setConfiguration(configuration);
//		return bean;
//	}


    @Bean // 将数据源纳入spring事物管理
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}
